package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserVehicle;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserVehicleDaoImpl extends TempDataObjectDao<UserVehicle> {

	public Map<Long,Map<String, UserVehicle>> getSomeUserVehicleMap(List<Long> userIdList) {
		Map<Long,Map<String, UserVehicle>> dataMap = new ConcurrentHashMap<>();
		if(userIdList.size() == 0){
			return dataMap;
		}
		String sql = "select * from t_u_vehicle where userId in ("+ StringUtils.listToString(userIdList,",") +") and isDelete=0";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicle data = parse(rs);
				if(!dataMap.containsKey(data.getUserId())){
					dataMap.put(data.getUserId(),new ConcurrentHashMap<>());
				}
				dataMap.get(data.getUserId()).put(data.getVehicleUid(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<String, UserVehicle> getUserVehicleMap(long userId) {
		String sql = "select * from t_u_vehicle where userId=? and isDelete=0";
		Map<String, UserVehicle> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicle data = parse(rs);
				dataMap.put(data.getVehicleUid(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserVehicle data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_vehicle(vehicleUid,userId,templateId,patronsId,level,breakLv,studyLv,equipTimes,equipBuyTimes,lastUpdateTime,"
				+ "isDelete,createTime,resetTimes,isLock,soulBlock,boneLv,boneHole) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getVehicleUid());
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getTemplateId());
			call.setInt(i++, data.getPatronsId());
			call.setInt(i++, data.getLevel());
			call.setInt(i++, data.getBreakLv());
			call.setInt(i++, data.getStudyLv());
			call.setInt(i++, data.getEquipTimes());
			call.setInt(i++, data.getEquipBuyTimes());
			call.setLong(i++, data.getLastUpdateTime());
			call.setBoolean(i++, data.isDelete());
			call.setLong(i++, data.getCreateTime());
			call.setInt(i++, data.getResetTimes());
			call.setBoolean(i++, data.isLock());
			call.setString(i++, StringUtils.listToString(data.getSoulBlockList(),";"));
			call.setInt(i++, data.getBoneLv());
			call.setInt(i++, data.getBoneHole());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserVehicle data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_vehicle SET userId=?,templateId=?,patronsId=?,level=?,breakLv=?,studyLv=?,equipTimes=?,equipBuyTimes=?,"
				+ "lastUpdateTime=?,isDelete=?,createTime=?,resetTimes=?,isLock=?,soulBlock=?,boneLv=?,boneHole=? WHERE vehicleUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getTemplateId());
			call.setInt(i++, data.getPatronsId());
			call.setInt(i++, data.getLevel());
			call.setInt(i++, data.getBreakLv());
			call.setInt(i++, data.getStudyLv());
			call.setInt(i++, data.getEquipTimes());
			call.setInt(i++, data.getEquipBuyTimes());
			call.setLong(i++, data.getLastUpdateTime());
			call.setBoolean(i++, data.isDelete());
			call.setLong(i++, data.getCreateTime());
			call.setInt(i++, data.getResetTimes());
			call.setBoolean(i++, data.isLock());
			call.setString(i++, StringUtils.listToString(data.getSoulBlockList(),";"));
			call.setInt(i++, data.getBoneLv());
			call.setInt(i++, data.getBoneHole());

			call.setString(i++, data.getVehicleUid());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserVehicle parseData(ResultSet rs) throws SQLException {
		UserVehicle data = new UserVehicle();
		data.setVehicleUid(rs.getString("vehicleUid"));
		data.setUserId(rs.getLong("userId"));
		data.setTemplateId(rs.getInt("templateId"));
		data.setPatronsId(rs.getInt("patronsId"));
		data.setLevel(rs.getInt("level"));
		data.setBreakLv(rs.getInt("breakLv"));
		data.setStudyLv(rs.getInt("studyLv"));
		data.setEquipTimes(rs.getInt("equipTimes"));
		data.setEquipBuyTimes(rs.getInt("equipBuyTimes"));
		data.setCreateTime(rs.getLong("createTime"));
		data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
		data.setDelete(rs.getBoolean("isDelete"));
		data.setResetTimes(rs.getInt("resetTimes"));
		data.setLock(rs.getBoolean("isLock"));
		String soulBlock = rs.getString("soulBlock");
		if(!StringUtils.isNullOrEmpty(soulBlock)){
			data.setSoulBlockList(StringUtils.stringToIntegerList(soulBlock,";"));
		}
		data.setBoneLv(rs.getInt("boneLv"));
		data.setBoneHole(rs.getInt("boneHole"));
		return data;
	}
}
